﻿@{
    Layout = "_LayoutAdmin";
}

@model PostEditViewModel

@section scripts{
    <script src="~/js/3rd/typeahead.bundle.min.js"></script>
    @{
        var ec = Settings.Value.Editor;
        switch (ec)
        {
            case EditorChoice.Html:
                <script src="~/lib/tinymce/tinymce.min.js"></script>
                break;
            case EditorChoice.Markdown:
                <script src="~/js/3rd/simplemde.min.js"></script>
                <script src="~/js/3rd/inline-attachment.js"></script>
                <script src="~/js/3rd/codemirror-4.inline-attachment.js"></script>
                <link id="css-simplemde" href="~/css/3rd/simplemde.min.css" rel="stylesheet" />
                break;
            case EditorChoice.None:
            default:
                break;
        }
    }

<script>
        $(function () {
            postEditor.initEvents();
            var editorMode = '@Settings.Value.Editor';
            if (editorMode == '@nameof(EditorChoice.Html)') {
                postEditor.loadRichEditor(".post-content-textarea");
            }
            if (editorMode == '@nameof(EditorChoice.Markdown)') {
                postEditor.loadMdEditor(".post-content-textarea");
            }

            postEditor.keepAlive();
        });
</script>
}

<form class="post-edit-form" asp-action="CreateOrEdit"
      data-ajax-begin="onPostCreateEditBegin"
      data-ajax-complete="onPostCreateEditComplete"
      data-ajax-failure="onPostCreateEditFailed"
      data-ajax-success="onPostCreateEditSuccess"
      data-ajax="true"
      data-ajax-method="POST">
    <div asp-validation-summary="ModelOnly" class="alert alert-danger"></div>
    <input type="hidden" asp-for="PostId" />
    <input type="hidden" asp-for="IsPublished" />
    <div class="row">
        <div class="col-md-9 col-xl-10">
            <input type="text" asp-for="Title" class="form-control form-control-lg" placeholder="@Localizer["Title"]" />
            <div>
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
            <div class="form-group">
                <div class="form-row">
                    <div class="col-10">
                        <div class="input-group input-group-sm">
                            <input type="text" asp-for="Slug" class="form-control form-control-sm" placeholder="Slug" />
                        </div>
                        <div>
                            <span asp-validation-for="Slug" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-2">
                        <input type="text" asp-for="ContentLanguageCode" class="form-control form-control-sm" placeholder="@Localizer["Language Code"]" />
                        <div>
                            <span asp-validation-for="ContentLanguageCode" class="text-danger"></span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <textarea asp-for="EditorContent" class="post-content-textarea"></textarea>
                <div>
                    <span asp-validation-for="EditorContent" class="text-danger"></span>
                </div>
            </div>

            <div class="form-group">
                <input type="text" asp-for="Tags" class="form-control" placeholder="@Localizer["Tags"]" />
            </div>
        </div>
        <div class="col-md-3 col-xl-2">
            <div class="card mb-4">
                <div class="card-header">@Localizer["Options"]</div>
                <ul class="list-group list-group-flush">
                    <li class="list-group-item">
                        <label asp-for="EnableComment" class="control-label"></label>
                        <div class="material-switch float-right">
                            <input @(Model.EnableComment ? "checked=\"checked\"" : "") id="EnableComment" name="EnableComment" type="checkbox" value="true">
                            <label for="EnableComment" class="label-success"></label>
                            <input name="EnableComment" type="hidden" value="false">
                        </div>
                    </li>
                    <li class="list-group-item">
                        <label asp-for="FeedIncluded" class="control-label"></label>
                        <div class="material-switch float-right">
                            <input @(Model.FeedIncluded ? "checked=\"checked\"" : "") id="FeedIncluded" name="FeedIncluded" type="checkbox" value="true">
                            <label for="FeedIncluded" class="label-success"></label>
                            <input name="FeedIncluded" type="hidden" value="false">
                        </div>
                    </li>
                    <li class="list-group-item">
                        <label asp-for="ExposedToSiteMap" class="control-label"></label>
                        <div class="material-switch float-right">
                            <input @(Model.ExposedToSiteMap ? "checked=\"checked\"" : "") id="ExposedToSiteMap" name="ExposedToSiteMap" type="checkbox" value="true">
                            <label for="ExposedToSiteMap" class="label-success"></label>
                            <input name="ExposedToSiteMap" type="hidden" value="false">
                        </div>
                    </li>
                </ul>
                @if (Model.PostId != Guid.Empty)
                {
                    <div class="card-body">
                        <a class="btn btn-light" data-toggle="modal" data-target="#changePublishDateModal">@Localizer["Change Publish Date"]</a>
                    </div>
                }
            </div>
            <div class="card">
                <div class="card-header">@Localizer["Categories"]</div>
                <div class="card-body">
                    @if (null != Model.CategoryList && Model.CategoryList.Any())
                    {
                        <div class="catlist">
                            <ul>
                                @foreach (var item in Model.CategoryList)
                                {
                                    <li>
                                        <div class="custom-control custom-checkbox">
                                            <input id="cat-@item.Value" name="SelectedCategoryIds" class="custom-control-input" type="checkbox" value="@item.Value"
                                                   @(item.IsChecked ? "checked=\"checked\"" : null)>
                                            <label for="cat-@item.Value" class="custom-control-label">@item.DisplayText</label>
                                        </div>
                                    </li>
                                }
                            </ul>
                        </div>
                    }
                </div>
                <div class="card-footer">
                    <a asp-action="Manage" asp-controller="Category" class="btn btn-sm btn-secondary">@Localizer["Manage Categories"]</a>
                </div>
            </div>
        </div>
    </div>

    <hr />

    <div>
        <button type="submit" class="btn btn-success ediblogeditor-save" id="btn-save">
            @Localizer["Save"]
        </button>
        @if (!Model.IsPublished)
        {
            <button type="submit" class="btn btn-primary ediblogeditor-publish" id="btn-publish">
                @Localizer["Publish"]
            </button>
            <button class="btn btn-outline-success ediblogeditor-preview" id="btn-preview">@Localizer["Preview"]</button>
        }
        <a asp-controller="Post" asp-action="Manage" class="btn btn-light">@Localizer["Cancel"]</a>
    </div>

    <div class="modal fade" id="changePublishDateModal" tabindex="-1" role="dialog" aria-labelledby="changePublishDateModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="resetModalLabel">@Localizer["Change Publish Date"]</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="alert alert-warning">@Localizer["Changing publish date will modify the URL of the post, and may cause unexpected results like breaking links on third parties that were pointing to this post. Please check the checkbox below if you confirm."]</div>
                    <div class="form-group">
                        <input asp-for="ChangePublishDate" type="checkbox" />
                        <label asp-for="ChangePublishDate" class="control-label"></label>
                    </div>

                    <div class="form-group">
                        <label asp-for="PublishDate" class="control-label"></label>
                        <input asp-for="PublishDate" class="form-control" />
                        <span asp-validation-for="PublishDate" class="text-danger"></span>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">@Localizer["Cancel"]</button>
                    <button type="button" class="btn btn-danger" data-dismiss="modal">@Localizer["Continue"]</button>
                </div>
            </div>
        </div>
    </div>
</form>